package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.ItemCat;
import com.mall.db.service.ItemCatService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ic")
public class ItemCatController extends BaseController<ItemCat> {

    @Resource(name = "itemCatService")
    private ItemCatService is;


    @RequestMapping("/getFind")
    public Map<String,Object>getFind(ItemCat itemCat){
        PageHelper.startPage(itemCat.getPage(),itemCat.getLimit());
        QueryWrapper qw=new QueryWrapper();
        qw.eq("pid",0);
        List<ItemCat> list=is.list(qw);
        PageInfo<ItemCat> pageInfo=new PageInfo<>(list,itemCat.getLimit());
        return this.findMap(pageInfo.getList(),pageInfo.getTotal());
    }

    @Override
    public Map<String, Object> find(ItemCat itemCat) {
        PageHelper.startPage(itemCat.getPage(),itemCat.getLimit());
        QueryWrapper qw = new QueryWrapper();
        List<ItemCat> list = is.list(qw);
        PageInfo<ItemCat> pageInfo = new PageInfo<>(list,itemCat.getLimit());
        return findMap(pageInfo.getList(),pageInfo.getTotal());
    }

    @Override
    public Map<String, Object> add(ItemCat itemCat) {
        boolean b=is.save(itemCat);
        return this.iud("新增",b,itemCat);
    }

    @RequestMapping("/findParent")
    public Map<String, Object> findParent(ItemCat itemCat){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("pid",0);
        List<ItemCat> list = is.list(qw);
        PageInfo<ItemCat> pageInfo = new PageInfo<>(list,itemCat.getLimit());
        return findMap(pageInfo.getList(),pageInfo.getTotal());
    }

    @Override
    public Map<String, Object> update(ItemCat itemCat) {
        boolean b=is.updateById(itemCat);
        return this.iud("编辑",b,itemCat);

    }

    @Override
    public Map<String, Object> delete(ItemCat itemCat) {
           boolean b = is.removeById(itemCat.getId());
        return this.iud("删除",b,itemCat);
    }
}
